package com.lagou.server;

import java.io.IOException;

import com.lagou.model.StudentMessage;
import com.lagou.model.UserMessage;

public class ServerView {
    /**
     * 实现服务器的界面功能
     */
    /**
     * 合成复用原则得到服务器端对象，以得到输入输出流
     */
    private ServerInitClose sic;
    private ServerDao sd;
    /**
     * 通过构造方法，实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法，实现管理员的登录校验
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        UserMessage um = (UserMessage) sic.getOis().readObject();
        System.out.println("接收到的对象信息为: " + um);
        if (sd.serverManageCheck(um.getUser())) {
            um.setType("success");
        } else {
            um.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(um);
        System.out.println("服务器发送校验结果成功");
    }

    /**
     * 自定义成员方法，实现学员的登录校验
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void serverStuReceive() throws IOException, ClassNotFoundException {
        StudentMessage stm = (StudentMessage) sic.getOis().readObject();
        System.out.println("接收到的对象信息为: " + stm);
        // 学号，姓名，密码都正确的情况下，登录成功，否则登录失败
        if (sd.serverStuCheck(stm.getStu().getId(), stm.getStu().getStuName(), stm.getStu().getStuPassword())) {
            stm.setType("success");
        } else {
            stm.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(stm);
        System.out.println("服务器发送校验结果成功");
    }
}
